package com.pet.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.pet.system.controller.vo.PetVo;
import com.pet.system.entity.Pet;
import com.pet.system.mapper.PetMapper;
import com.pet.system.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class PetServiceImpl implements PetService {
    @Autowired
    private PetMapper PetMapper;

    @Override
    public Pet getInfo(Long id) {
        return PetMapper.selectById(id);
    }

    @Override
    public PageInfo<Pet> getPageList(PetVo pet) {
        LambdaQueryWrapper<Pet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(null!=pet.getSex(),Pet::getSex,pet.getSex())
                .eq(StringUtil.isNotEmpty(pet.getType()),Pet::getType,pet.getType())
        ;

        return PageHelper.startPage(pet.getPageNo(), pet.getPageSize())
                .doSelectPageInfo(() -> PetMapper.selectList(queryWrapper));
    }

    @Override
    public int remove(Long[] ids) {
        for (Long id : ids) {
            int i = PetMapper.deleteById(id);
        }
        return 1;
    }

    @Override
    public int edit(Pet pet) {
        return PetMapper.updateById(pet);
    }

    @Override
    public int add(Pet pet) {
        return PetMapper.insert(pet);
    }
}
